#ifndef RECORDDATABASE_H
#define RECORDDATABASE_H

#include "Database.h"

#include <QList>

class Record
{
public:
    Record();

    int recordId;

    float temperature, weight, bloodGlucose;
    int bloodPressureSystolic, bloodPressureDiastolic,
        respiratoryRate, pulseRate;

    QString commentPatient, commentDoctor;
    QDateTime observationDate;
};

class RecordDatabaseModel : public QAbstractTableModel
{
public:
    RecordDatabaseModel();
    void setSource(QList<Record> *records);

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;

    void sort(int column, Qt::SortOrder order);

    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

private:
    int rowNum, colNum;
    QList<Record> *records;
};

/*!
 * @addtogroup Database
 * @{
 * @class RecordDatabase
 * @brief Manages medical records of patients.
 *
 * The RecordDatabase is used to store and retrieve medical records of
 * patients.
 * @}
 */

class RecordDatabase : protected Database
{
public:
    RecordDatabase();
    ~RecordDatabase();

    /*! Loads medical records of patient with specified user id.
     * @note To get a list of the loaded medical records, user the getRecords()
     *       function.
     */
    void loadRecords(int userId);

    /*! Adds medical record to specified patient's records.
     * @param record Pointer to Record object containing record information.
     * @param userId User id of patient associated with the record.
     */
    void addRecord(const Record * record, int userId);

    /*! Adds comment to existing record.
     * @param record Pointer to Record object containing record information.
     * @param recordId Id of the record to update.
     */
    void addComment(const QString comment, int recordId);

    /*! Returns model of currently loaded record list for use with QListView.
     * @note Ownership of the RecordDatabaseModel belongs to RecordDatabase,
     *       so an instance of RecordDatabase should remain valid as long as
     *       the QListView is displaying the RecordDatabaseModel.
     */
    RecordDatabaseModel * getModel();

    QList<Record> * getRecords();
    Record getRecordById(int recordId);
    int getRecordCount();

private:
    void initDatabase();
    Record parseQuery(const QSqlQuery &query) const;

    template<typename T>
    void bindNumCheck(QSqlQuery &query, T value);

    int recordCount;
    QList<Record> records;

    RecordDatabaseModel * model;
};

#endif // RECORDDATABASE_H
